package com.leyou.user.controller;

import com.leyou.user.pojo.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author shkstart
 * @create 2020-05-08 19:38
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //ajax判断用户名或手机号是否存在
    @GetMapping("check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable(value = "type") Integer type) {
        Boolean bool = this.userService.checkData(data, type);
        if (bool == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(bool);
    }
    //发送验证码
    @PostMapping("send")
    public ResponseEntity<Void> sendPhone(String phone){
        userService.send(phone);

        return ResponseEntity.ok().build();
    }
    //注册
    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, String code){
        Boolean register = userService.register(user, code);
        if (!register) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //登录
    @PostMapping("query")
    public ResponseEntity<User> queryUser(@RequestParam("username")String username,@RequestParam("password")String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User u = userService.queryUser(user);
        if (u == null){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(u);
    }
}
